package com.ltedu.system.service.impl;

import com.ltedu.system.service.SysMenuService;
import com.ltedu.system.service.SysPermissionService;
import com.ltedu.system.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * @className: SysPermissionServiceImpl
 * @auther: Jerry
 * @date: 2020/12/1 11:46
 * @desctiption: TODO
 * @version: 1.0
 */
@Service
public class SysPermissionServiceImpl implements SysPermissionService {

    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    SysMenuService sysMenuService;

    /**
     * 获取用户角色权限
     *
     * @param userId 用户id
     * @return {@link Set<String>}
     */
    @Override
    public Set<String> getRolePermission(Long userId) {
        Set<String> roles = new HashSet<>();
        // 管理员拥有所有权限
        if (sysRoleService.isAdmin(userId)) {
            roles.add("admin");
        } else {
            roles.addAll(sysRoleService.RolePermissionByUserId(userId));
        }
        return roles;
    }

    /**
     * 获取用户菜单权限
     *
     * @param userId 用户id
     * @return {@link Set<String>}
     */
    @Override
    public Set<String> getMenuPermission(Long userId) {
        Set<String> perms = new HashSet<>();
        if (sysRoleService.isAdmin(userId)) {
            perms.add("*:*:*");
        } else {
            perms.addAll(sysMenuService.getMenuPermsByUserId(userId));
        }
        return perms;
    }
}
